package com.imooc.service.impl;

import com.imooc.common.dto.OrderDTO;
import com.imooc.common.enums.ResultEnum;
import com.imooc.common.exception.SelllException;
import com.imooc.common.utils.JsonUtil;
import com.imooc.service.OrderService;
import com.imooc.service.PayService;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

@Slf4j
@Service
public class PayServiceImpl implements PayService {

    private static final String ORDER_NAME = "微信支付点餐";

    @Autowired
    BestPayServiceImpl bestPayService;

    @Autowired
    OrderService orderService;

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderAmount(Double.valueOf(orderDTO.getOrderAmount().toString()));
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);

        log.info("微信支付请求参数：{}", JsonUtil.toJson(payRequest));
        PayResponse response = bestPayService.pay(payRequest);
        log.info("微信支付返回结果：{}", JsonUtil.toJson(response));

        return response;
    }

    @Override
    public PayResponse wechatNotify(String notifyData) {
        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("微信异步通知返回结果：{}", JsonUtil.toJson(payResponse));

        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());

        if (Objects.isNull(orderDTO)) {
            log.error("微信通知  订单不存在，orderId = {}", payResponse.getOrderId());
            throw new SelllException(ResultEnum.ORDER_NOT_EXIST);
        }

        if (orderDTO.getOrderAmount().compareTo(new BigDecimal(payResponse.getOrderAmount().toString())) != 0) {
            log.error("微信通知  金额不一致，orderId = {} , 订单金额 = {}, 通知返回金额 = {}",
                    payResponse.getOrderId(), orderDTO.getOrderAmount(), payResponse.getOrderAmount());
            throw new SelllException(ResultEnum.ORDER_AMOUNT_NOT_MATCH_ERROR);
        }

        orderService.paid(orderDTO);
        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("微信退款请求参数：{}", JsonUtil.toJson(JsonUtil.toJson(refundRequest)));
        RefundResponse refundResponse = bestPayService.refund(refundRequest);
        log.info("微信退款返回结果：{}", JsonUtil.toJson(JsonUtil.toJson(refundResponse)));
        return refundResponse;
    }
}
